package org.csu.assemblyroom.service.impl;

import org.csu.assemblyroom.common.CommonResponse;
import org.csu.assemblyroom.entity.Admin;
import org.csu.assemblyroom.persistence.AdminMapper;
import org.csu.assemblyroom.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public CommonResponse<Admin> login(String username, String password) {
        Admin admin=adminMapper.selectById(username);
        if(admin!=null)
        {
            if(admin.getPassword().equals(password))
            {
                return CommonResponse.createForSuccessMessage("登陆成功");
            }
            else {
                return CommonResponse.createForError("用户名或密码错误");
            }
        }
        else
        {
            return CommonResponse.createForError("无此用户");
        }
    }

    @Override
    public CommonResponse<Admin> register(String username, String password,String repassword) {
        Admin admin=adminMapper.selectById(username);
        if(admin!=null)
        {
            return CommonResponse.createForError("该用户名已存在");
        }
        else
        {
            if(password.equals(repassword))
            {
                admin=new Admin();
                admin.setAdmin(username);
                admin.setPassword(password);
                adminMapper.insert(admin);
                return CommonResponse.createForSuccessMessage("注册成功");
            }
            else {
                return CommonResponse.createForError("两次密码不一致");
            }

        }
    }
}
